package com.aier.school.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.Classes;
import com.aier.classes.entity.RequestParent;
import com.aier.classes.entity.RequestTeacher;
import com.aier.classes.service.IClassService;
import com.aier.classes.service.IClassUserService;
import com.aier.classes.service.IInviteRequestService;
import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolGrade;
import com.aier.school.entity.SchoolMessage;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.service.ISchoolService;
import com.aier.school.service.ISchoolTeacherService;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("schoolHomePageAction")
public class SchoolHomePageAction extends SchoolBaseAction{

    /**
     * 学校首页
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "schoolService")
    private ISchoolService schoolService;    
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;    
    @Resource(name = "classService")
    private IClassService classService;    
    @Resource(name = "inviteRequestService")
    private IInviteRequestService inviteRequestService;    
    @Resource(name = "userService")
    private IUserService userService;    
    @Resource(name = "classUserService")
    private IClassUserService classUserService;        
    
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
    private Integer in_type;    //进入类型(1:学校班级页,2:学校公告页,3:学校教师页)
    
    private List<SchoolGrade> schoolGradeList;    //职务列表    
    private List<Classes> classList;            //班级列表
    private List<SchoolTeacher> schoolTeacherList;    //学校教师列表
    private List<SchoolMessage> schoolMessageList;    //学校公告列表
    
    private Integer sid;        //学校id
    private SchoolTeacher schoolTeacher;    //学校教师
    private School school;        //学校
    private Long cid;            //班级id
    private Long baby_uid;        //宝宝uid
    private String baby_name;    //宝宝姓名
    private String parentname;    //家长和宝宝关系
    private String relation;    //家长和宝宝关系
    private Long gid;            //职务id
    private String tname;        //教师姓名
    private String content;        //留言
    private Long uid;
	private int page;            //页数
    private int page_size;        //每页的记录数
    private int page_count;        //总页数
    private int count;            //总数
    private String strcid;          //多个班级字符串用逗号隔开			


	//=============================================================================================    
    @Override
    public String execute(){           
        school = schoolService.searchSchoolBySid(sid);
        classList = classService.searchClassList(sid);
        schoolGradeList = schoolService.searchSchoolGradeList(sid);
        if(in_type!=null&&in_type==1){//学校班级页
            classList = classService.searchClassList_Num(sid);
        }
        if(in_type!=null&&in_type==2){//学校公告页
            schoolMessageList = schoolService.searchSchoolMessageList(sid);
        }
        if(in_type!=null&&in_type==Constant.num_3){//学校教师页
            schoolTeacherList = schoolTeacherService.searchSchoolTeacherList(sid);
        }        
        return SUCCESS;
    }
    
    /**
     * 根据学校ID获取职务列表
     * @return
     */
    public String getSchoolGrade(){
    	schoolGradeList = schoolService.searchSchoolGradeList(sid);
    	s=1;
    	return SUCCESS;
    }
    
    //进入教师管理和考勤之前的判断
    public String isSchoolTeacher(){
        Long uid = (Long) session.get(CommonSession.SESSION_ACCOUNT_ID);
        schoolTeacher = schoolTeacherService.searchSchoolTeacherByIds(uid,sid);
        if(schoolTeacher==null){
            s = 0;
        }else{
            s = 1;
        }
        return SUCCESS;
    }
    
    //用户 申请加入学校 -家长
    public String addRequest_parent(){
        Timestamp ctime = new Timestamp(new Date().getTime());
        //Long uid = (Long) session.get(CommonSession.SESSION_ACCOUNT_ID);
        User baby_user = userService.insertUserBabyByCondition(uid,baby_name,relation);
        RequestParent requestParent = new RequestParent(null, uid, baby_user.getUid(), relation, null, cid, content, null, ctime,parentname);
        int student_num = classUserService.countClassStudentByCondition(baby_user.getUid(),null);
        if(student_num>0){
            s = 0;
            error_info = "您的宝宝已经在一个班级了";
        }else{
            s = inviteRequestService.insertRequestParent(requestParent);
        }
        return SUCCESS;
    }
    
    //用户 申请加入学校 -教师
    public String addRequest_teacher(){
        Timestamp ctime = new Timestamp(new Date().getTime());
       
        if(schoolTeacherService.searchSchoolTeacherByIds(uid, null)!=null){
            s = 0;
            error_info = "您已经是教师了";
        }else{
        	String[] temp=strcid.split(",");
        	for (int i = 0; i < temp.length; i++) {
        		RequestTeacher requestTeacher = new RequestTeacher(null, uid, tname, gid, sid,Long.parseLong(temp[i]), null, content, null, ctime);
        		s = inviteRequestService.insertRequestTeacher(requestTeacher);
        	}
        	s=1;
        }
        return SUCCESS;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public List<SchoolGrade> getSchoolGradeList() {
        return schoolGradeList;
    }

    public List<Classes> getClassList() {
        return classList;
    }

    public List<SchoolTeacher> getSchoolTeacherList() {
        return schoolTeacherList;
    }

    public List<SchoolMessage> getSchoolMessageList() {
        return schoolMessageList;
    }

    public SchoolTeacher getSchoolTeacher() {
        return schoolTeacher;
    }

    public School getSchool() {
        return school;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public void setInviteRequestService(IInviteRequestService inviteRequestService) {
        this.inviteRequestService = inviteRequestService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public void setBaby_uid(Long baby_uid) {
        this.baby_uid = baby_uid;
    }

    public void setBaby_name(String baby_name) {
        this.baby_name = baby_name;
    }

    public void setRelation(String relation) {
        this.relation = relation;
    }

    public void setGid(Long gid) {
        this.gid = gid;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setClassUserService(IClassUserService classUserService) {
        this.classUserService = classUserService;
    }

    public void setIn_type(Integer in_type) {
        this.in_type = in_type;
    }    
    
    public void setStrcid(String strcid) {
		this.strcid = strcid;
    }

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public void setParentname(String parentname) {
		this.parentname = parentname;
	}
    


}
